/**
 * 
 */
package com.jie.eclipse.pan.gef.action;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.ToolBarManager;
import org.eclipse.jface.viewers.CheckboxTableViewer;
import org.eclipse.swt.widgets.TableItem;

import com.jie.eclipse.pan.dao.QMDao;
import com.jie.eclipse.pan.data.BaseDataEntity;
import com.jie.eclipse.pan.swtitem.actiongroup.BaseActionGroup;
import com.jie.eclipse.pan.swtitem.table.PTable;
import com.jie.eclipse.pan.swtitem.table.PTableViewer;
import com.jie.eclipse.pan.utils.Utils;

/**
 * @author yanjie
 *
 */
public class SelectedColumnsActionGroup extends BaseActionGroup{
	
	public SelectedColumnsActionGroup(Object obj,String key,String action) {
		super(obj, key,action);
	}
	
	private class AddRowAction extends Action{
		public AddRowAction() {
			this.setImageDescriptor(getImageDesc("icon_addrow.gif"));
			this.setText("新增行");
		}
		
		@Override
		public void run() {
			CheckboxTableViewer ctv2 = (CheckboxTableViewer)getData().gpov(getKey()+"ctv2");//已选栏位表格选择器
			PTableViewer tv2 =  (PTableViewer)getData().gpov(getKey()+"tv2");//已选栏位表格选择器
			
			if(ctv2 == null){
				Utils.print("表格选择器不正常");return;
			}
			
			BaseDataEntity row = new BaseDataEntity();
			if("BasePanelDialog".equals(getAction())){
				row.spsv("id", tv2.getNextId());
				row.spsv("name", tv2.getNextId());
				row.spsv("width", "100");
				row.spsv("caption", "新增列");
				row.spsv("xtype", "text");
				row.spsv("xtype_text", "文本框");
				row.spsv("required", "false");
				row.spsv("required_text", "Y");
				row.spsv("detail", "配置");
				row.spov("detail_img","icons/gef/eclipse/thread_view.gif");
			}else if("TableDialog".equals(getAction())){
				row.spsv("id", tv2.getNextId());
				row.spsv("name", tv2.getNextId());
				row.spsv("caption", "新增列");
				row.spsv("xtype", "--");
				row.spsv("xtype_text", "");
				row.spsv("detail", "配置");				
				row.spov("detail_img","icons/gef/eclipse/thread_view.gif");
				row.spsv("editortype", "static");
				row.spsv("editortype_text", "静态");
			}else if("Crud1LeftPage_condition".equals(getAction())){
				row.spsv("id", tv2.getNextId());
				row.spsv("name", tv2.getNextId());
				row.spsv("width", "100");
				row.spsv("caption", "新增列");
				row.spsv("xtype", "text");
				row.spsv("xtype_text", "文本框");
				row.spsv("required", "false");
				row.spsv("required_text", "Y");
				row.spsv("detail", "配置");
				row.spov("detail_img","icons/gef/eclipse/thread_view.gif");
				
				row.spsv("op", "=");
				row.spsv("op_text", "等于");
				row.spsv("tablepix", "");
				row.spsv("columnalias", row.gpsv("cid").toLowerCase());
			}else if("Crud1LeftPage_grid".equals(getAction())){
				row.spsv("id", tv2.getNextId());
				row.spsv("name", tv2.getNextId());
				row.spsv("width", "100");
				row.spsv("caption", "新增列");
			}else if("Crud1RightPage_form".equals(getAction())){
				row.spsv("id", tv2.getNextId());
				row.spsv("name", tv2.getNextId());
				row.spsv("width", "100");
				row.spsv("xtype", "text");
				row.spsv("xtype_text", "文本框");
				row.spsv("caption", "新增列");
				row.spsv("detail", "配置");				
				row.spov("detail_img","icons/gef/eclipse/thread_view.gif");
				row.spsv("editortype", "static");
				row.spsv("editortype_text", "静态");
			}if("GridCrudPage_condition".equals(getAction())){
				row.spsv("id", tv2.getNextId());
				row.spsv("name", tv2.getNextId());
				row.spsv("width", "100");
				row.spsv("caption", "新增列");
				row.spsv("xtype", "text");
				row.spsv("xtype_text", "文本框");
				row.spsv("required", "false");
				row.spsv("required_text", "Y");
				row.spsv("detail", "配置");
				row.spov("detail_img","icons/gef/eclipse/thread_view.gif");
			}else if("GridCrudPage_grid".equals(getAction())){
				row.spsv("id", tv2.getNextId());
				row.spsv("name", tv2.getNextId());
				row.spsv("caption", "新增列");
				row.spsv("xtype", "--");
				row.spsv("xtype_text", "");
				row.spsv("editortype", "static");
				row.spsv("editortype_text", "静态");
				row.spsv("detail", "配置");				
				row.spov("detail_img","icons/gef/eclipse/thread_view.gif");
			}
			
			((PTable)ctv2.getTable()).addRecord(row);
		}
	}
	
	private class DeleteRowAction extends Action{
		public DeleteRowAction() {
			this.setImageDescriptor(getImageDesc("icon_delete.gif"));
			this.setText("删除行");
		}
		
		@Override
		public void run() {
			CheckboxTableViewer ctv2 = (CheckboxTableViewer)getData().gpov(getKey()+"ctv2");//已选栏位表格选择器
			if(ctv2 == null){
				Utils.print("表格选择器不正常");
				return;
			}
			ctv2.remove(ctv2.getCheckedElements());
		}
	}
	
	private class ICaptionAction extends Action{
		public ICaptionAction() {
			this.setImageDescriptor(getImageDesc("ext/fam/user_suit.gif"));
			this.setText("标题智能翻译");
		}
		
		@Override
		public void run() {
			CheckboxTableViewer ctv2 = (CheckboxTableViewer)getData().gpov(getKey()+"ctv2");//已选栏位表格选择器
			if(ctv2 == null){
				Utils.print("表格选择器不正常");
				return;
			}
			BaseDataEntity icaption  = QMDao.getInstance().getICaption();
			for (int j = 0; j < ctv2.getTable().getItems().length; j++) {
				TableItem tabitem = ctv2.getTable().getItems()[j];
				BaseDataEntity row = (BaseDataEntity)tabitem.getData();
				String name = row.gpsv("name");
				if(icaption.ContainesParam(name)){
					row.spsv("caption", icaption.gpsv(name));
					tabitem.setText(1,icaption.gpsv(name));
				}
			}
		}
	}
	
	private class Insert2Action extends Action{
		public Insert2Action() {
			this.setImageDescriptor(getImageDesc("checkboxon.gif"));
			this.setText("全选");
		}
		
		@Override
		public void run() {
			CheckboxTableViewer ctv2 = (CheckboxTableViewer)getData().gpov(getKey()+"ctv2");//已选栏位表格选择器 
			if(ctv2 == null){
				Utils.print("表格选择器不正常");
				return;
			}
			
			for (int i = 0; i < ctv2.getTable().getItems().length; i++) {
				TableItem item = ctv2.getTable().getItems()[i];
				item.setChecked(true);
			}
		}
	}
	
	private class Insert3Action extends Action{
		public Insert3Action() {
			this.setImageDescriptor(getImageDesc("checkboxoff.gif"));
			this.setText("全否");
		}
		
		@Override
		public void run() {
			CheckboxTableViewer ctv2 = (CheckboxTableViewer)getData().gpov(getKey()+"ctv2");//已选栏位表格选择器
			if(ctv2 == null){
				Utils.print("表格选择器不正常");
				return;
			}
			
			for (int i = 0; i < ctv2.getTable().getItems().length; i++) {
				TableItem item = ctv2.getTable().getItems()[i];
				item.setChecked(false);
			}
			
		}
	}
	
	private class Insert4Action extends Action{
		public Insert4Action() {
			this.setImageDescriptor(getImageDesc("checkboxoff.gif"));
			this.setText("反选");
		}
		
		@Override
		public void run() {
			CheckboxTableViewer ctv2 = (CheckboxTableViewer)getData().gpov(getKey()+"ctv2");//已选栏位表格选择器
			if(ctv2 == null){
				Utils.print("表格选择器不正常");
				return;
			}
			
			for (int i = 0; i < ctv2.getTable().getItems().length; i++) {
				TableItem item = ctv2.getTable().getItems()[i];
				item.setChecked(!item.getChecked());
			}
			
		}
	}
	
	private class Insert5Action extends Action{
		public Insert5Action() {
			this.setImageDescriptor(getImageDesc("checkboxoff.gif"));
			this.setText("显示行号");
		}
		
		@Override
		public void run() {
			CheckboxTableViewer ctv2 = (CheckboxTableViewer)getData().gpov(getKey()+"ctv2");//已选栏位表格选择器
			PTableViewer tv2 =  (PTableViewer)getData().gpov(getKey()+"tv2");//已选栏位表格选择器
			
			if("显示行号".equals(this.getText())){
				BaseDataEntity row = new BaseDataEntity();
				row.spsv("id", "rownumber");
				row.spsv("name", "rownumber");
				row.spsv("width", "30");
				row.spsv("caption", "序號");
				row.spsv("xtype", "");
				row.spsv("xtype_text", "--");
				((PTable)ctv2.getTable()).addRecord(row);
				this.setImageDescriptor(getImageDesc("checkboxon.gif"));
				this.setText("关闭行号");
			}else{
				for (int i = 0; i < ctv2.getTable().getItems().length; i++) {
					TableItem item = ctv2.getTable().getItems()[i];
					if("rownumber".equals(((BaseDataEntity)item.getData()).gpsv("id")))
						ctv2.remove(item.getData());
				}
				this.setImageDescriptor(getImageDesc("checkboxoff.gif"));
				this.setText("显示行号");
			}
			
//			CheckboxTableViewer ctv2 = (CheckboxTableViewer)getData().gpov(getKey()+"ctv2");//已选栏位表格选择器
//			if(ctv2 == null){
//				Utils.print("表格选择器不正常");
//				return;
//			}
//			
//			for (int i = 0; i < ctv2.getTable().getItems().length; i++) {
//				TableItem item = ctv2.getTable().getItems()[i];
//				item.setChecked(!item.getChecked());
//			}
			
		}
	}
	
	public void fillActionToolbars (ToolBarManager actionBarManager,boolean brownumber){
		AddRowAction addrowAction = new AddRowAction();
		DeleteRowAction deleterowAction = new DeleteRowAction();
		ICaptionAction icaptionAction = new ICaptionAction();
		
		actionBarManager.add(createActionContributionItem(addrowAction));
		actionBarManager.add(createActionContributionItem(deleterowAction));
		actionBarManager.add(createActionContributionItem(icaptionAction));
		
		Insert2Action action2 = new Insert2Action();
		actionBarManager.add(createActionContributionItem(action2));
		Insert3Action action3 = new Insert3Action();
		actionBarManager.add(createActionContributionItem(action3));
		Insert4Action action4 = new Insert4Action();
		actionBarManager.add(createActionContributionItem(action4));
		if(brownumber){
			Insert5Action action5 = new Insert5Action();
			actionBarManager.add(createActionContributionItem(action5));
		}
		actionBarManager.update(true);
	}
}

